package com.rockcent.api.controller;

import com.rockcent.api.Routes;
import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.MallFinancialSettings;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.MallFinancialSettingsRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 财务设置
 * Created by zhangjh on 2017/3/29.
 */
@RestController
@RequestMapping(value = Routes.API_VERSION)
public class MallFinancialSettingsController extends BaseController {
    private final Logger log = LoggerFactory.getLogger(MallFinancialSettingsController.class);

    @Autowired
    private MallFinancialSettingsRepository mallFinancialSettingsRepository;


    @Autowired
    private BaseUserHelper userHelper;


    /**
     * @api {get} /v1/merchant/mall/financial/settings/info M 财务设置详情
     * @apiGroup merchant
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiSuccess {long} id ID
     * @apiSuccess {long} mallId 平台ID
     * @apiSuccess {BigDecimal} probationDays 试用期天数
     * @apiSuccess {BigDecimal} baseServiceFee 基础服务费
     * @apiSuccess {BigDecimal} businessServiceFeeRate 商户服务费系数
     * @apiSuccess {BigDecimal} shareFeeRate 分享返利率
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data" :
     *          {
     *              "id" : 1
     *              "mallId" : 51,
     *              "probationDays" : 30,
     *              "baseServiceFee" : 0.11,
     *              "businessServiceFeeRate" : 0.1,
     *              "shareFeeRate" : 0.02,
     *          }
     * }
     */
    @RequestMapping(value = Routes.MERCHANT_MALL_FINANCIAL_SETTINGS_INFO, method = RequestMethod.GET)
    public JsonResult mallPayStandardInfo(HttpServletRequest request) {
        RestDoing doing = jsonResult -> {

            UserInfoDto userInfoDto = userHelper.getSession(request);

            MallFinancialSettings mallFinancialSettings = mallFinancialSettingsRepository.findTop1ByMallIdAndIsDeleteOrderByLastUpdatedDesc(userInfoDto.mall.getId(),Whether.NO);
            jsonResult.data = mallFinancialSettings;
        };
        return doing.go(request, log);
    }

    /**
     * @api {put} /v1/merchant/mall/financial/settings/update M 财务设置更改
     * @apiGroup merchant
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiParam {long} probationDays <code>必传参数</code> 试用期天数
     * @apiParam {BigDecimal} baseServiceFee <code>必传参数</code> 基础服务费
     * @apiParam {BigDecimal} businessServiceFeeRate <code>必传参数</code> 商户服务费系数
     * @apiParam {BigDecimal} shareFeeRate <code>必传参数</code> 分享返利率
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *   "code": "1",
     *   "desc": "成功"
     */
    @RequestMapping(value = Routes.MERCHANT_MALL_FINANCIAL_SETTINGS_UPDATE, method = RequestMethod.PUT)
    public JsonResult mallPayStandardUpdate(HttpServletRequest request, @RequestBody MallFinancialSettings dto) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {



            if (dto.getBusinessServiceFeeRate() == null) {
                jsonResult.remind("基础服务费系数 不能为空",log);
                return;
            }
//            if (dto.getBaseServiceFee() == null) {
//                jsonResult.remind("基础服务费不能为空",log);
//                return;
//            }
            if (dto.getShareFeeRate() == null) {
                jsonResult.remind("分享返利率不能为空",log);
                return;
            }
            Long mallId = userInfoDto.mall.getId();
            MallFinancialSettings mallFinancialSettings = mallFinancialSettingsRepository.findTop1ByMallIdAndIsDeleteOrderByLastUpdatedDesc(mallId,Whether.NO);
            MallFinancialSettings newMallFinancialSettings = new MallFinancialSettings();
            if (null != mallFinancialSettings) {
                mallFinancialSettings.setIsDelete(Whether.YES);
                mallFinancialSettingsRepository.save(mallFinancialSettings);
            }
            newMallFinancialSettings.setBaseServiceFee(dto.getBaseServiceFee());
            newMallFinancialSettings.setBusinessServiceFeeRate(dto.getBusinessServiceFeeRate());
            newMallFinancialSettings.setMallId(mallId);
            newMallFinancialSettings.setProbationDays(dto.getProbationDays());
            newMallFinancialSettings.setShareFeeRate(dto.getShareFeeRate());

            mallFinancialSettingsRepository.save(newMallFinancialSettings);

            jsonResult.data = newMallFinancialSettings;
        };
        return doing.go(request, log);
    }

}
